<?php
/**
 * @version		$Id: categories.php 18695 2010-08-28 17:18:48Z infograf768 $
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
 * This models supports retrieving lists of article categories.
 *
 * @package		Joomla.Administrator
 * @subpackage	com_skkn
 * @since		1.6
 */
class skknModelCategories extends JModel
{
	/**
	 * Model context string.
	 *
	 * @var		string
	 */
	public $_context = 'com_skkn.texts';

	/**
	 * The category context (allows other extensions to derived from this model).
	 *
	 * @var		string
	 */
	protected $_extension = 'com_skkn';

	private $_items = null;
	
	/**
	 * @param	boolean	True to join selected foreign information
	 *
	 * @return	string
	 * @since	1.6
	 */
	function getItems()
	{
		// Create a new query object.
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('*')
				->from('#__categories')
				->where('extension = "'.$this->_extension.'"')
		;
		
		$db->setQuery($query);
		$result = $db->loadObjectList();
		
		if($db->getErrorMsg())
			die($db->getErrorMsg());
		
		$query = $db->getQuery(true);
		
		foreach ($result as & $category)
		{
			$query->select('s.*, level.name AS level_name, subject.name AS subject_name')
					->from('#__skkn s')
					->join('LEFT', '#__skkn_info level ON level.id = s.level_id')
					->join('LEFT', '#__skkn_info subject ON subject.id = s.subject_id')
					->where('s.catid = ' . (int) $category->id)
					->order('s.id DESC');
			;
			
			$db->setQuery($query, 0, 3);
			$s = $db->loadObjectList();
			
			if($db->getErrorMsg())
				die($db->getErrorMsg());
			
			$category->files = $s;
		}
				
		return $result;
	}
	
	function getInfo($type = 'level')
	{
		$db =& JFactory::getDbo();
		$query = "SELECT * FROM #__skkn_info WHERE info_type = '$type' AND state = 1";
		
		$db->setQuery($query);
		
		return $db->loadObjectList();
	}
}
